<?php

class Pushandplay_Controller_Extenders_XML extends Zend_Controller_Action 
{
	public $output;
	
	public $config;
	public $auth;
	public $authAdapter;
	public $params;
	public $dbAdapter;
	public $fileAdapter;
	public $cache;
	public $cache_id;
	public $acl;
	public $logger;
	
	public $validatorEmail;
	public $validatorDate;
	public $validatorString;
	public $validatorNumber;
	public $validatorNotEmpty;
	
	public function preDispatch()
	{
		$this->config				= Zend_Registry::get('Zend_Config_Ini');
		$this->cache				= Zend_Registry::get('Zend_Cache');
		$this->auth					= Zend_Auth::getInstance();
		$this->authAdapter			= Zend_Registry::get('Zend_Auth');
		$this->fileAdapter			= Zend_Registry::get('Zend_File');
		$this->acl 					= Zend_Registry::get('Zend_Acl');
		$this->logger 				= Zend_Registry::get('Zend_Log');
		$this->dbAdapter			= Zend_Db_Table::getDefaultAdapter();
		$this->params 				= $this->getRequest()->getParams();	
		$this->cache_id				= $this->params['module'].'_'.$this->params['controller'].'_'.$this->params['action'];
		$this->acl_id				= $this->params['module'].':'.$this->params['controller'].':'.$this->params['action'];
		
		$this->validatorDate		= new Zend_Validate_Date();
		$this->validatorEmail		= new Zend_Validate_EmailAddress();
		$this->validatorString		= new Zend_Validate_StringLength();
		$this->validatorNumber      = new Zend_Validate_Digits();
		$this->validatorNotEmpty	= new Zend_Validate_NotEmpty();
		
		self::setStatus();
		
	    try
    	{
    		if(!$this->acl->isAllowed($this->auth->getIdentity()->role, $this->acl_id))
    		{
    		    self::_redirect('error/accessdenied');
    		}
    	}
    	catch (Zend_Exception $e) {}
	}
	public function postDispatch()
    {
		//$this->_helper->layout->setLayout('xml');
		
    	$this->getResponse()->setHeader('Content-Type', 'text/xml; charset=UTF-8', true); 
    	$this->getResponse()->setHeader('Content-Transfer-Encoding: binary\n', true); 
    }
    
    /**
     * 
     * @param Number $status_code
     * @param String $status_string
     */
    protected function setStatus($status_code = NULL, $status_string = NULL)
    {
    	if (!isset($status_code)) 	$status_code	= $this->config->errors->success->code;
    	if (!isset($status_string))	$status_string	= $this->config->errors->success->message;
    	
    	$this->view->placeholder('errorNumber')->set($status_code);
		$this->view->placeholder('errorDescription')->set($status_string);
		$this->view->placeholder('role')->set($this->auth->getIdentity()->role);
    }
    
    /**
     * 
     * @param String $fromEmail
     * @param String $fromName
     * @param String $toEmail
     * @param String $toName
     * @param String $subject
     * @param String $body
     */
    protected function sendEmail($fromEmail, $fromName, $toEmail, $toName, $subject, $body)
    {
    	$mail = new Zend_Mail('UTF-8');
		//$mail->setEncodingOfHeaders('utf-8');
		//$mail->setBodyText('My Nice Test Text');
		$mail->setBodyHtml($body, 'UTF-8', Zend_Mime::ENCODING_BASE64);
		$mail->setFrom($fromEmail, $fromName);
		$mail->addTo($toEmail, $toName);
		$mail->setSubject($subject);
		$mail->send();
    }
}

?>